package com.heisen.uua.service.impl;

import com.heisen.base.constant.HttpConstant;
import com.heisen.redis.utils.RedisUtils;
import com.heisen.uua.constant.UuaConstant;
import com.heisen.uua.exception.ValidateCodeException;
import com.heisen.uua.service.ValidateCodeService;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author sen
 * @description
 * @date 2023/12/26 16:19
 */
@Slf4j
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 生成验证码
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @SneakyThrows
    public void create(HttpServletRequest request, HttpServletResponse response) {
        String key = request.getParameter("key");
        if (StringUtils.isBlank(key)) {
            throw new ValidateCodeException(HttpConstant.HttpMsg.MSG_VERIFICATION_KEY_NOT_FIN);
        }
        setHeader(response, UuaConstant.type);
        Captcha captcha = createCaptcha();  //创建验证码
        redisUtils.setForObj(UuaConstant.RedisPrefix.CODE_PREFIX + key, StringUtils.lowerCase(captcha.text()), UuaConstant.time);
        captcha.out(response.getOutputStream());
    }



    /**
     * 校验验证码
     *
     * @param key   前端上送 key
     * @param code 前端上送待校验值
     */
    public void check(String key, String code) throws ValidateCodeException {
        String validateCodeKey = UuaConstant.RedisPrefix.CODE_PREFIX + key;
        Object codeInRedis = redisUtils.getForKey(validateCodeKey);
        if (StringUtils.isBlank(code)) {
            throw new ValidateCodeException(HttpConstant.HttpMsg.MSG_VERIFICATION_CODE_NOT_FIND);
        }
        if (codeInRedis == null) {
            throw new ValidateCodeException(HttpConstant.HttpMsg.MSG_VERIFICATION_CODE_XSE);
        }
        if (!StringUtils.equalsIgnoreCase(code, String.valueOf(codeInRedis))) {
            redisUtils.delForKey(validateCodeKey);
            throw new ValidateCodeException(HttpConstant.HttpMsg.MSG_VERIFICATION_CODE_WRONG);
        }
        // 删除验证码
        redisUtils.delForKey(validateCodeKey);
    }

    private Captcha createCaptcha() {
        Captcha captcha = null;
        if (StringUtils.equalsIgnoreCase(UuaConstant.type, UuaConstant.GIF)) {
            captcha = new GifCaptcha(UuaConstant.width, UuaConstant.height, UuaConstant.length);
        } else {
            captcha = new SpecCaptcha(UuaConstant.width, UuaConstant.height, UuaConstant.length);
        }
        captcha.setCharType(UuaConstant.charType);
        return captcha;
    }

    private void setHeader(HttpServletResponse response, String type) {
        if (StringUtils.equalsIgnoreCase(type, UuaConstant.GIF)) {
            response.setContentType(MediaType.IMAGE_GIF_VALUE);
        } else {
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
        }
        response.setHeader(HttpHeaders.PRAGMA, "No-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
        response.setDateHeader(HttpHeaders.EXPIRES, 0L);
    }


}
